<script setup lang="ts">
import { computed, onMounted, reactive, ref } from 'vue'
import type { FormRules } from 'element-plus'
import { getLeaveWordById } from '@/api/interface/message/feedback'

interface Props {
  data?: Record<string, any> | never | null
  status: string
}

const props = withDefaults(defineProps<Props>(), {
  data: null,
  status: 'add'
})

const formRef = ref()

const form = reactive<Record<string, any>>({})

const rules = reactive<FormRules>({
  // customerId: [{ required: true, message: '客户不能为空!', trigger: 'change' }]
})

const initForm = () => {
  if (props.data) {
    if (props.status !== 'add') {
      getLeaveWordById({ id: props.data.id }).then((res: any) => {
        Object.keys(res).forEach((key: string) => {
          form[key] = props.data![key]
        })
      })
    } else {
      Object.keys(props.data).forEach((key: string) => {
        form[key] = props.data![key]
      })
    }
  }
}

const formatForm = () => {
  const result: Record<string, any> = {}
  Object.keys(form).forEach((key: string) => {
    result[key] = form[key]
  })
  return result
}

const submitForm = () => {
  return new Promise<any>((resolve) => {
    formRef.value.validate((valid: boolean) => {
      let res: any = null
      if (valid) {
        res = formatForm()
      }
      resolve(res)
    })
  })
}

const disabled = computed(() => {
  return props.status != 'add' && props.status != 'edit'
})

defineExpose({
  submitForm
})

onMounted(() => {
  initForm()
})
</script>

<template>
  <div class="form-wrapper">
    <el-form ref="formRef" :model="form" :rules="rules" label-width="100px" class="seedball-due-detail-form">
      <el-form-item label="留言时间：" prop="createTime">
        <el-date-picker
          v-model="form.createTime"
          type="datetime"
          range-separator="-"
          value-format="YYYY-MM-DD HH:mm:ss"
          placeholder="留言时间"
          :disabled="disabled"
        />
      </el-form-item>
      <el-form-item label="用户昵称：" prop="userName">
        <el-input v-model="form.userName" type="text" :maxlength="20" :disabled="disabled" placeholder="请输入用户昵称" />
      </el-form-item>
      <el-form-item label="留言内容：" prop="content">
        <el-input v-model="form.content" :rows="3" type="textarea" :disabled="disabled" placeholder="请输入备注" />
      </el-form-item>
    </el-form>
  </div>
</template>

<style lang="scss" scoped>
.seedball-due-detail-form {
  width: 500px;
  flex-wrap: wrap;
}
</style>
